//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.model.wills.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class KyContractDao extends BaseDao<KyContract, Integer> {
    public KyContractDao() {
    }

    public List<KyContract> getListByMemberCode(String memberCode) {
        return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).asc("endTime"), -1);
    }

    public List<KyContract> getValidListByMemberCode(String memberCode, String shopCode) {
        return this.queryList("memberCode=:p1 and shopCode=:p2 and factAmount+presentAmount-useAmount>0 and outTime is null and (overDueUse=1 or endTime>=convert(varchar(10), getdate(),120))", new Object[]{memberCode, shopCode}, (new OrderSet()).asc("endTime"), -1);
    }

    public KyContract getValidAmountByMemberCode(String memberCode, String shopCode) {
        String sql = "select sum(case when factAmount>useAmount then factAmount-useAmount else 0 end) factAmount,sum(case when factAmount>useAmount then presentAmount else factAmount+presentAmount-useAmount end) presentAmount,max(endTime) endTime from KyContract where memberCode=:p1 and shopCode=:p2 and factAmount+presentAmount-useAmount>0 and outTime is null and (overDueUse=1 or endTime>=convert(varchar(10), getdate(),120))";
        KyContract contract = (KyContract)this.getBySql(KyContract.class, sql, new Object[]{memberCode, shopCode});
        if(contract != null && contract.getFactAmount() == null) {
            contract = null;
        }

        return contract;
    }

    public Integer getCountByMemberCode(String memberCode) {
        String sql = "select count(*) nn from KyContract where memberCode=:p1";
        return (Integer)this.getBySql(Integer.class, sql, new Object[]{memberCode});
    }

    public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select c.SaleId,c.ShopCode,c.MemberCode,c.EarnestId,c.PromotionId,c.SaleType,c.SaleDate,c.FactAmount,c.PresentAmount,c.EarnestAmount,c.InAmount,c.PayAmount,c.BeginTime,c.EndTime,c.UseAmount,c.OverDueUse,c.Note,c.Status,c.ApproveUser,c.ApproveTime,c.OutAmount,c.OutType,c.OutTime,c.SignStatus,c.PrintLabel,c.PrintTime,c.PrintUser,c.SalesCode,c.SalesCode2,c.createUser,c.cardCode,m.chName memberName\nfrom KyContract c inner join MemberInfo m on c.memberCode=m.memberCode\n" + (sqlw.length() > 0?" where " + sqlw:"") + " order by c.saleDate desc";
        this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
    }

    public void addContract(KyContract contract, MemberInfo member, List<PayList> payList) {
        String shopCode = contract.getShopCode();
        String sql = "select max(contractCode) from KyContract where shopCode=:p1 and contractCode like :p2";
        String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
        int maxId = 0;
        if(maxCode != null && maxCode.length() > shopCode.length()) {
            try {
                maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
            } catch (Exception var11) {
                ;
            }
        }

        String contractCode = "000000" + (maxId + 1);
        contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
        contract.setContractCode(contractCode);
        this.add(contract);
        if(member.getKyogaCard() == null || !member.getKyogaCard().equalsIgnoreCase(contract.getCardCode())) {
            member.setKyogaCard(contract.getCardCode());
            sql = "update KyCard set memberCode=:p1, status=2, receiveUser=:p2, receiveDate=getdate() where cardCode=:p3";
            this.updateBySql(sql, new Object[]{member.getMemberCode(), contract.getCreateUser(), contract.getCardCode()});
            sql = "update MemberInfo set kyogaCard=:p1 where memberCode=:p2";
            this.updateBySql(sql, new Object[]{contract.getCardCode(), member.getMemberCode()});
        }

        Iterator memberName = payList.iterator();

        while(memberName.hasNext()) {
            PayList sign = (PayList)memberName.next();
            sign.setSaleId(contract.getSaleId());
            sign.setContractCode(contract.getContractCode());
            this.getSession().save(sign);
            if("S011".equalsIgnoreCase(sign.getPayType())) {
                sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
                this.updateBySql(sql, new Object[]{Integer.valueOf(2), sign.getAutoId(), sign.getSalesCode(), sign.getSalesCode2(), new Date(), sign.getNote()});
            }
        }

        if(contract.getEarnestId() != null) {
            sql = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
            this.updateBySql(sql, new Object[]{contract.getSaleId(), contract.getEarnestId()});
        }

        String memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{contract.getMemberCode()});
        ContractSign sign1 = new ContractSign();
        sign1.setShopCode(contract.getShopCode());
        sign1.setContractCode(contract.getContractCode());
        sign1.setContractType("09");
        sign1.setSaleId(contract.getSaleId());
        sign1.setMemberCode(contract.getMemberCode());
        sign1.setMemberName(memberName1 == null?"":memberName1);
        sign1.setStatus(Integer.valueOf(1));
        sign1.setSalesCode(contract.getSalesCode());
        sign1.setSalesCode2(contract.getSalesCode2());
        sign1.setCreateUser(contract.getCreateUser());
        sign1.setCreateTime(new Date());
        sign1.setUpdateTime(new Date());
        this.getSession().save(sign1);
    }
}
